package net.mfjassociates.financial.expense.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import net.mfjassociates.financial.expense.client.managed.request.ApplicationRequestFactory;
import net.mfjassociates.financial.expense.client.managed.request.ReportDetailProxy;
import net.mfjassociates.financial.expense.client.managed.request.ReportDetailRequest;
import net.mfjassociates.financial.expense.client.managed.request.TaxCodeProxy;
import net.mfjassociates.financial.expense.client.managed.ui.ReportDetailDetailsView;
import net.mfjassociates.financial.expense.client.managed.ui.ReportDetailEditView;
import net.mfjassociates.financial.expense.client.managed.ui.ReportDetailListView;
import net.mfjassociates.financial.expense.client.managed.ui.ReportDetailMobileDetailsView;
import net.mfjassociates.financial.expense.client.managed.ui.ReportDetailMobileEditView;
import net.mfjassociates.financial.expense.client.scaffold.ScaffoldApp;
import net.mfjassociates.financial.expense.client.scaffold.place.CreateAndEditProxy;
import net.mfjassociates.financial.expense.client.scaffold.place.FindAndEditProxy;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyPlace;

public class ReportDetailActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public ReportDetailActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new ReportDetailDetailsActivity((EntityProxyId<ReportDetailProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? ReportDetailMobileDetailsView.instance() : ReportDetailDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<net.mfjassociates.financial.expense.client.managed.request.ReportDetailProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<ReportDetailProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        ReportDetailEditView.instance().setCreating(true);
        final ReportDetailRequest request = requests.reportDetailRequest();
        Activity activity = new CreateAndEditProxy<ReportDetailProxy>(ReportDetailProxy.class, request, ScaffoldApp.isMobile() ? ReportDetailMobileEditView.instance() : ReportDetailEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ReportDetailProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new ReportDetailEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ReportDetailMobileEditView.instance() : ReportDetailEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        ReportDetailEditView.instance().setCreating(false);
        EntityProxyId<ReportDetailProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<ReportDetailProxy>(proxyId, requests, ScaffoldApp.isMobile() ? ReportDetailMobileEditView.instance() : ReportDetailEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ReportDetailProxy proxy) {
                ReportDetailRequest request = requests.reportDetailRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new ReportDetailEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ReportDetailMobileEditView.instance() : ReportDetailEditView.instance(), activity, proxyId);
    }
}
